﻿// WebZHX.API/Controllers/BaseController.cs
using Common.Models;
using Core.Interfaces;
using Infrastructure.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace WebZHX.Controllers
{
    /// <summary>
    /// 基础控制器
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TKey">主键类型</typeparam>
    [ApiController]
    [Route("api/[controller]")]
    public class BaseController<TEntity, TKey> : ControllerBase where TEntity : class, IEntity<TKey>
    {
        protected readonly BaseService<TEntity, TKey> _service;

        public BaseController(BaseService<TEntity, TKey> service)
        {
            _service = service;
        }

        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        [HttpGet("{id}")]
        public virtual async Task<ActionResult<TEntity>> Get(TKey id)
        {
            var entity = await _service.GetByIdAsync(id);
            if (entity == null) return NotFound();
            return Ok(entity);
        }

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns>实体列表</returns>
        [HttpGet]
        public virtual async Task<ActionResult<IEnumerable<TEntity>>> GetAll()
        {
            var entities = await _service.GetAllAsync();
            entities = entities.Where(w => w.IsDeleted == false).ToList();
            return Ok(entities);
        }

        /// <summary>
        /// 新增实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>新增的实体</returns>
        [HttpPost]
        public virtual async Task<ActionResult<TEntity>> Create(TEntity entity)
        {
            var result = await _service.CreateAsync(entity);
            return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">实体</param>
        /// <returns>更新后的实体</returns>
        [HttpPut("{id}")]
        public virtual async Task<ActionResult<TEntity>> Update(TKey id, TEntity entity)
        {
            if (!id.Equals(entity.Id)) return BadRequest();

            var result = await _service.UpdateAsync(entity);
            return Ok(result);
        }

        /// <summary>
        /// 局部更新实体（只更新非null字段）
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">实体</param>
        /// <returns>更新后的实体</returns>
        [HttpPut("PartialUpdate/{id}")]
        public virtual async Task<ActionResult<TEntity>> PartialUpdate(TKey id, TEntity entity)
        {
            if (!id.Equals(entity.Id)) return BadRequest();

            var result = await _service.PartialUpdateAsync(entity);
            return Ok(result);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>无内容</returns>
        [HttpDelete("{id}")]
        public virtual async Task<ActionResult> Delete(TKey id)
        {
            var result = await _service.DeleteAsync(id);
            if (!result) return NotFound();
            return NoContent();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns>分页结果</returns>
        [HttpGet("paged")]
        public virtual async Task<ActionResult<PagedResult<TEntity>>> GetPaged(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 20,
            [FromQuery] string orderBy = "Id")
        {
            var result = await _service.GetPagedAsync(pageIndex, pageSize, orderBy);
            return Ok(result);
        }

        /// <summary>
        /// 分页查询（带过滤条件）
        /// </summary>
        /// <param name="filter">查询过滤器</param>
        /// <returns>分页结果</returns>
        [HttpPost("paged")]
        public virtual async Task<ActionResult<PagedResult<TEntity>>> GetPaged(QueryFilter filter)
        {
            var result = await _service.GetPagedAsync(filter);
            return Ok(result);
        }
    }
}